package com.muqi.app.user.widget;

import java.util.List;

import com.bumptech.glide.Glide;
import com.easemob.easeui.widget.CircularImage;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.db.ChildBean;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener;

public class ChildrenWindow extends PopupWindow implements OnDismissListener {
	private View conentView;
	private Activity mContext;
	private LinearLayout mGridview;
	private List<ChildBean> myChildren;
	private HorizontalScrollView hs;
	public interface ChildrenListener {
		void onSeleectChild(ChildBean child);
	}

	private ChildrenListener selectListener;

	@SuppressWarnings("deprecation")
	public ChildrenWindow(final Activity context, ChildrenListener listener,
			List<ChildBean> children) {
		this.selectListener = listener;
		this.mContext = context;
		this.myChildren = children;
		this.setOnDismissListener(this);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.popwindow_children, null);
		// int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		this.setContentView(conentView);
		this.setWidth(2*w/3);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.AnimationPreview);

		//mGridview = (LinearLayout) conentView.findViewById(R.id.childLayout);
		hs = (HorizontalScrollView) conentView.findViewById(R.id.hs);
		mGridview= new LinearLayout(mContext);
		LinearLayout.LayoutParams params=new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		//params.gravity=Gravity.CENTER;
		mGridview.setLayoutParams(params);
		hs.addView(mGridview);
	}

	private void drawMyGridLayout(String currentId) {
		mGridview.removeAllViews();
		LinearLayout.LayoutParams params=null;
		for (int i = 0; i < myChildren.size(); i++) {
			final int sonPosition = i;
			View convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_user_avatr, null);
			CircularImage itemAvatar = (CircularImage) convertView
					.findViewById(R.id.mCircularImage);
			TextView itemName = (TextView) convertView
					.findViewById(R.id.mUserName);
			ImageView isChecked = (ImageView) convertView
					.findViewById(R.id.mChecked);
			params=new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
			params.leftMargin=5;
			params.rightMargin=5;
			convertView.setLayoutParams(params);
			Glide.with(mContext).load(myChildren.get(i).avatar_02)
					.dontAnimate().placeholder(R.drawable.default_avator)
					.into(itemAvatar);
			itemName.setText(myChildren.get(i).name + "");
			if (currentId.equals(myChildren.get(i).child_id)) {
				isChecked.setVisibility(View.VISIBLE);
			}
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					drawMyGridLayout(myChildren.get(sonPosition).child_id);
					selectListener.onSeleectChild(myChildren.get(sonPosition));
					ChildrenWindow.this.dismiss();
				}
			});
			mGridview.addView(convertView);
		}
	}

	/**
	 * showPopupWidow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent, String curretChildId) {
		if (!this.isShowing()) {
			drawMyGridLayout(curretChildId);
			this.showAtLocation(parent, Gravity.CENTER, 0, 0);
			backgroundAlpha(0.5f);
			this.setFocusable(true);
			this.setOutsideTouchable(true);
			this.update();
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onDismiss() {
		backgroundAlpha(1f);
	}

	public void backgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		mContext.getWindow().setAttributes(lp);
	}

}
